 

package com.zdkj.subway.rca.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.subway.rca.entity.SwCkDiffAfterClear;
import com.zdkj.subway.rca.service.SwCkClearRulesService;
import com.zdkj.subway.rca.service.SwCkDiffAfterClearService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * 清分及划账-清分后数据差异管理
 *
 * @author stw
 * @date 2021-01-03 21:22:15
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swckdiffafterclear" )
@Api(value = "swckdiffafterclear", tags = "清分及划账-清分后数据差异管理")
public class SwCkDiffAfterClearController {

    @Autowired
    private SwCkDiffAfterClearService swCkDiffAfterClearService;
    @Autowired
    private SwCkClearRulesService swCkClearRulesService;

    /**
     * 运营商下拉列表
     * @return
     */
    @ApiOperation(value = "运营商下拉列表", notes = "运营商下拉列表")
    @GetMapping("/listoperators" )
    public R listOperators() {
        return R.ok(swCkClearRulesService.getServiceMap());
    }

    /**
     * 票种下拉列表
     * @return
     */
    @ApiOperation(value = "票种下拉列表", notes = "票种下拉列表")
    @GetMapping("/listtickettype" )
    public R listTicketType() {
        return R.ok(swCkDiffAfterClearService.getTicketTypeMap());
    }
    /**
     * 调整原因下拉列表
     * @return
     */
    @ApiOperation(value = "调整原因下拉列表", notes = "调整原因下拉列表")
    @GetMapping("/listadjustreason" )
    public R listAdjustReason() {
        return R.ok(swCkDiffAfterClearService.getAdjustReasonMap());
    }

    /**
     * 查询
     * @return
     */
    @ApiOperation(value = "查询", notes = "查询")
    @GetMapping("/page" )
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "operators", value = "运营商编码", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "lineCode", value = "线路编码", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "startDate", value = "周期日期：yyyy-MM-dd", dataTypeClass = String.class,required = true),
                    @ApiImplicitParam(name = "endDate", value = "周期日期：yyyy-MM-dd", dataTypeClass = String.class,required = true)
            }
    )
    public R getSwCkDiffAfterClearPage(String operators,String lineCode,String startDate,String endDate) {
        if(StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)){
            R.failed("周期不能为空");
        }
        List<SwCkDiffAfterClear> list = swCkDiffAfterClearService.getSelect(operators,lineCode,startDate,endDate);
        return R.ok(list);
    }


    /**
     * 通过id查询清分后数据差异
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(swCkDiffAfterClearService.getById(id));
    }


    /**
     * 修改清分后数据差异
     * @param swCkDiffAfterClear 清分后数据差异
     * @return R
     */
    @ApiOperation(value = "修改清分后数据差异", notes = "修改清分后数据差异")
    @SysLog("修改清分后数据差异" )
    @PutMapping("/updateById")
    @PreAuthorize("@pms.hasPermission('sw_ck_diff_after_clear_edit')" )
    public R updateById(@RequestBody SwCkDiffAfterClear swCkDiffAfterClear) {
        SwCkDiffAfterClear clear = new SwCkDiffAfterClear();
        clear.setId(swCkDiffAfterClear.getId());
        clear.setAdjustReason(swCkDiffAfterClear.getAdjustReason());
        return R.ok(swCkDiffAfterClearService.updateById(clear));
    }

    /**
     * 清分及划账-清分后数据差异管理-确认
     * @return R
     */
    @ApiOperation(value = "清分及划账-清分后数据差异管理-确认", notes = "清分及划账-清分后数据差异管理-确认")
    @SysLog("清分及划账-清分后数据差异管理-确认" )
    @GetMapping({"/confirm"})
    @PreAuthorize("@pms.hasPermission('sw_ck_diff_after_clear_confirm')" )

    public R confirm(@RequestParam(value = "ids") List<Long> ids) {
        try {
            if (ids == null || ids.isEmpty()){
                return R.failed("必须选择id");
            }

            return R.ok(swCkDiffAfterClearService.confirm(ids));
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed();
        }

    }

    /**
     * 批量更新调整说明
     * @return R
     */
    @ApiOperation(value = "批量更新调整说明(id必选)", notes = "批量更新调整说明(id必选)")
    @SysLog("清分及划账-清分后数据差异管理-批量更新调整说明" )
    @PutMapping("/updateBatch")
    @PreAuthorize("@pms.hasPermission('sw_ck_diff_after_clear_edit_batch')" )
    public R updateBatch(@RequestBody List<SwCkDiffAfterClear> swCkDiffAfterClearList) {
        List<SwCkDiffAfterClear> list = new ArrayList<>();
        for (SwCkDiffAfterClear swCkDiffAfterClear : swCkDiffAfterClearList) {
            SwCkDiffAfterClear clear = new SwCkDiffAfterClear();
            clear.setId(swCkDiffAfterClear.getId());
            clear.setAdjustReason(swCkDiffAfterClear.getAdjustReason());
            list.add(clear);
        }
        return R.ok(swCkDiffAfterClearService.updateBatchById(list));
    }

    /**
     * 导出
     * @param
     * @return
     */
    @ApiOperation(value = "导出(上次清分日期和票种 必须选择）,", notes = "导出(上次清分日期和票种 必须选择）")
    @GetMapping({"/export"})
    @SysLog("清分及划账-清分后数据差异管理-导出")
    @PreAuthorize("@pms.hasPermission('sw_ck_diff_after_clear_export')")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "operators", value = "运营商编码", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "lineCode", value = "线路编码", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "startDate", value = "周期日期：yyyy-MM-dd", dataTypeClass = String.class,required = true),
                    @ApiImplicitParam(name = "endDate", value = "周期日期：yyyy-MM-dd", dataTypeClass = String.class,required = true)
            }
    )
    public void export(String operators,String lineCode,String startDate,String endDate, HttpServletResponse response) {
        swCkDiffAfterClearService.export(operators,lineCode,startDate,endDate,response);
    }
}
